package com.gupao.vip.netty;

import com.gupao.vip.netty.entity.SubscribeRequest;
import com.gupao.vip.netty.entity.SubscribeResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class SubReqServerHandler extends ChannelInboundHandlerAdapter {
    private static int counter;
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SubscribeRequest req = (SubscribeRequest)msg;
        if("majingang".equalsIgnoreCase(req.getUserName())){
            System.out.println("Service accept client subscribe req : [" + req + "]");
            SubscribeResponse res = new SubscribeResponse();
            res.setSubReqID(req.getSubReqID());
            res.setResponseCode(0);
            res.setDesc((++counter) + ". Netty book order succeed, 3 days later, sent to the designated address");
            ctx.writeAndFlush(res);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.err.println(cause.getMessage());
        //发生异常，关闭链路
        ctx.close();
    }
}
